#!/bin/sh
#
# cronmon        Startup script for cronmon
#
# chkconfig: - 85 15
# processname: cronmon
# pidfile: /var/run/uwsgi_cronmon.pid
# description: cronmon is a scheduled task monitoring tool
#
### BEGIN INIT INFO
# Provides: cronmon
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop cronmon
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -L $0 ]; then
    initscript=`/bin/readlink -f $0`
else
    initscript=$0
fi

sysconfig=`/bin/basename $initscript`

if [ -f /etc/sysconfig/$sysconfig ]; then
    . /etc/sysconfig/$sysconfig
fi

# Get cronmon init config.
. /etc/profile

basedir=/data/cronmon  # Change it as needed
venvdir=cronmon_venv  # Change it as needed
cronmon=${UWSGI-${basedir}/${venvdir}/bin/uwsgi}
prog=`/bin/basename $cronmon`
conffile=${CONFFILE:-${basedir}/cronmon.ini}
lockfile=${LOCKFILE:-/var/lock/subsys/cronmon}
pidfile=${PIDFILE:-/var/run/uwsgi_cronmon.pid}
RETVAL=0

start() {
    echo -n $"Starting $prog: "
    
    cd ${basedir}  # for mule config file
    daemon --pidfile ${pidfile} ${cronmon} --venv ${basedir}/${venvdir} --ini ${conffile}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} ${prog} -HUP
    RETVAL=$?
    echo
}


rh_status() {
    status -p ${pidfile} ${cronmon}
}

# See how we were called.
case "$1" in
    start)
        #rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        stop
        start
        ;;
    reload)
        reload
        ;;
    *)
        echo $"Usage: cronmon {start|stop|restart|reload|status|help}"
        RETVAL=2
esac

exit $RETVAL
